/****************************************************************************
 * Copyright (C) 2010
 * by Dimok
 * Modified by Dj_Skual
 * for SaveGame Manager GX
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 ***************************************************************************/
#include "ControlsSettingsMenu.hpp"
#include "../../Themes/CTheme.h"
#include "../../Controls/Application.h"
#include "../../Prompts/PromptWindows.h"
#include "../../Prompts/ControlsSetupWindow.h"

ControlsSettingsMenu::ControlsSettingsMenu()
	: SettingsBrowser()
{
	BtnSoundClick = Resources::GetSound("button_click.wav");
	BtnSoundOver = Resources::GetSound("button_over.wav");
	BtnSoundClick->SetVolume(Settings.SFXVolume);
	BtnSoundOver->SetVolume(Settings.SFXVolume);
	
	trigA.SetSimpleTrigger(-1, WiiControls.ClickButton | ClassicControls.ClickButton << 16, GCControls.ClickButton);
	
	resetBtnTxt = new GuiText(tr("Reset"), thInt("24 - settings menu back button text size"), thColor("r=0 g=0 b=0 a=255 - settings menu back button text color"));
	resetBtn = new PictureButton("settings_menu_button.png", "settings_menu_button.png", BtnSoundClick, BtnSoundOver);
	resetBtn->SetAlignment(thAlign("center - control settings menu reset button align hor") | thAlign("top - control settings menu reset button align ver"));
	resetBtn->SetPosition(thInt("195 - control settings menu reset button pos x"), thInt("400 - control settings menu reset button pos y"));
	resetBtn->SetLabel(resetBtnTxt);
	resetBtn->SetTrigger(&trigA);
	resetBtn->Clicked.connect(this, &ControlsSettingsMenu::OnResetButtonClick);
	Append(resetBtn);
	
	SetupOptionName();
	show();
}

ControlsSettingsMenu::~ControlsSettingsMenu()
{
	RemoveAll();
	
	delete resetBtnTxt;
	delete resetBtn;
	
	Resources::Remove(BtnSoundClick);
	Resources::Remove(BtnSoundOver);
}

void ControlsSettingsMenu::OnResetButtonClick(GuiButton *sender UNUSED, int pointer UNUSED, const POINT &p UNUSED)
{
	int choice = WindowPrompt(tr("Do you want to reset the control settings?"), 0, tr("Yes"), tr("Cancel"));
	if(choice)
	{
		Settings.Controls.SetDefault();
		SetOptionValues();
		optionBrowser->Refresh();
	}
}

void ControlsSettingsMenu::SetupOptionName()
{
	int Idx = 0;
	
	options->SetName(Idx++, tr("Screen Pointer Speed"));
	options->SetName(Idx++, tr("Click Button"));
	options->SetName(Idx++, tr("Click Button"));
	options->SetName(Idx++, tr("Click Button"));
	options->SetName(Idx++, tr("Back Button"));
	options->SetName(Idx++, tr("Back Button"));
	options->SetName(Idx++, tr("Back Button"));
	options->SetName(Idx++, tr("Up Button"));
	options->SetName(Idx++, tr("Up Button"));
	options->SetName(Idx++, tr("Up Button"));
	options->SetName(Idx++, tr("Down Button"));
	options->SetName(Idx++, tr("Down Button"));
	options->SetName(Idx++, tr("Down Button"));
	options->SetName(Idx++, tr("Left Button"));
	options->SetName(Idx++, tr("Left Button"));
	options->SetName(Idx++, tr("Left Button"));
	options->SetName(Idx++, tr("Right Button"));
	options->SetName(Idx++, tr("Right Button"));
	options->SetName(Idx++, tr("Right Button"));
	options->SetName(Idx++, tr("Home Button"));
	options->SetName(Idx++, tr("Home Button"));
	options->SetName(Idx++, tr("Home Button"));
	options->SetName(Idx++, tr("Parent Directory"));
	options->SetName(Idx++, tr("Parent Directory"));
	options->SetName(Idx++, tr("Parent Directory"));
	//!!here we can add onescroll and keyboard Special Keys buttons
	
	SetOptionValues();
}

void ControlsSettingsMenu::SetOptionValues()
{
	int Idx = 0;

	options->SetValue(Idx++, "%0.3f", Settings.PointerSpeed);

	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiMote, WiiControls.ClickButton).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiClassic, ClassicControls.ClickButton << 16).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeGCPad, GCControls.ClickButton).c_str());

	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiMote, WiiControls.BackButton).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiClassic, ClassicControls.BackButton << 16).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeGCPad, GCControls.BackButton).c_str());

	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiMote, WiiControls.UpButton).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiClassic, ClassicControls.UpButton << 16).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeGCPad, GCControls.UpButton).c_str());

	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiMote, WiiControls.DownButton).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiClassic, ClassicControls.DownButton << 16).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeGCPad, GCControls.DownButton).c_str());

	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiMote, WiiControls.LeftButton).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiClassic, ClassicControls.LeftButton << 16).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeGCPad, GCControls.LeftButton).c_str());

	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiMote, WiiControls.RightButton).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiClassic, ClassicControls.RightButton << 16).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeGCPad, GCControls.RightButton).c_str());

	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiMote, WiiControls.HomeButton).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiClassic, ClassicControls.HomeButton << 16).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeGCPad, GCControls.HomeButton).c_str());

	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiMote, WiiControls.UpInDirectory).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeWiiClassic, ClassicControls.UpInDirectory << 16).c_str());
	options->SetValue(Idx++, "%s", AppControls::ControlButtonsToString(AppControls::TypeGCPad, GCControls.UpInDirectory).c_str());
}

void ControlsSettingsMenu::OnControlsSetupWindowClose(GuiFrame *frame UNUSED)
{
	SetOptionValues();
	this->SetState(STATE_DEFAULT);
	optionBrowser->Refresh();
}

void ControlsSettingsMenu::OnBrowserClick(GuiOptionBrowser * browser UNUSED, int option)
{
	ControlsSetupWindow *window = 0;
	
	switch(option)
	{
		case 0:
			char entrie[20];
			snprintf(entrie, sizeof(entrie), "%0.3f", Settings.PointerSpeed);
			if(OnScreenNumpad(entrie))
				Settings.PointerSpeed = atof(entrie);
			break;
		case 1:
			window = new ControlsSetupWindow(AppControls::TypeWiiMote, &WiiControls.ClickButton);
			break;
		case 2:
			window = new ControlsSetupWindow(AppControls::TypeWiiClassic, &ClassicControls.ClickButton);
			break;
		case 3:
			window = new ControlsSetupWindow(AppControls::TypeGCPad, &GCControls.ClickButton);
			break;
		case 4:
			window = new ControlsSetupWindow(AppControls::TypeWiiMote, &WiiControls.BackButton);
			break;
		case 5:
			window = new ControlsSetupWindow(AppControls::TypeWiiClassic, &ClassicControls.BackButton);
			break;
		case 6:
			window = new ControlsSetupWindow(AppControls::TypeGCPad, &GCControls.BackButton);
			break;
		case 7:
			window = new ControlsSetupWindow(AppControls::TypeWiiMote, &WiiControls.UpButton);
			break;
		case 8:
			window = new ControlsSetupWindow(AppControls::TypeWiiClassic, &ClassicControls.UpButton);
			break;
		case 9:
			window = new ControlsSetupWindow(AppControls::TypeGCPad, &GCControls.UpButton);
			break;
		case 10:
			window = new ControlsSetupWindow(AppControls::TypeWiiMote, &WiiControls.DownButton);
			break;
		case 11:
			window = new ControlsSetupWindow(AppControls::TypeWiiClassic, &ClassicControls.DownButton);
			break;
		case 12:
			window = new ControlsSetupWindow(AppControls::TypeGCPad, &GCControls.DownButton);
			break;
		case 13:
			window = new ControlsSetupWindow(AppControls::TypeWiiMote, &WiiControls.LeftButton);
			break;
		case 14:
			window = new ControlsSetupWindow(AppControls::TypeWiiClassic, &ClassicControls.LeftButton);
			break;
		case 15:
			window = new ControlsSetupWindow(AppControls::TypeGCPad, &GCControls.LeftButton);
			break;
		case 16:
			window = new ControlsSetupWindow(AppControls::TypeWiiMote, &WiiControls.RightButton);
			break;
		case 17:
			window = new ControlsSetupWindow(AppControls::TypeWiiClassic, &ClassicControls.RightButton);
			break;
		case 18:
			window = new ControlsSetupWindow(AppControls::TypeGCPad, &GCControls.RightButton);
			break;
		case 19:
			window = new ControlsSetupWindow(AppControls::TypeWiiMote, &WiiControls.HomeButton);
			break;
		case 20:
			window = new ControlsSetupWindow(AppControls::TypeWiiClassic, &ClassicControls.HomeButton);
			break;
		case 21:
			window = new ControlsSetupWindow(AppControls::TypeGCPad, &GCControls.HomeButton);
			break;
		case 22:
			window = new ControlsSetupWindow(AppControls::TypeWiiMote, &WiiControls.UpInDirectory);
			break;
		case 23:
			window = new ControlsSetupWindow(AppControls::TypeWiiClassic, &ClassicControls.UpInDirectory);
			break;
		case 24:
			window = new ControlsSetupWindow(AppControls::TypeGCPad, &GCControls.UpInDirectory);
			break;
		default:
			break;
	}

	if(window != 0)
	{
		window->Closing.connect(this, &ControlsSettingsMenu::OnControlsSetupWindowClose);
		window->DimBackground(true);
		this->SetState(STATE_DISABLED);
		
		Application::Instance()->Append(window);
		Application::Instance()->SetUpdateOnly(window);
	}
	
	SetOptionValues();
	optionBrowser->Refresh();
}
